package mc.support.orm.shard.sql;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import mc.support.orm.shard.ShardStrategy;
import mc.support.orm.shard.sql.comm.AbstractSqlPhaser;

/**
 * Created with IntelliJ IDEA. <br/>
 * Author: Francis Yuen    <br/>
 * Date: 2014-10-15  <br/>
 */
public class UpdateSqlPhaser extends AbstractSqlPhaser {

    @Override
    protected SQLStatement doPhase( SQLStatement statement, Object params, String mapperId, ShardStrategy strategy ) {
        SQLUpdateStatement update = (SQLUpdateStatement) statement;
        SQLExprTableSource tableSource = (SQLExprTableSource) update.getTableSource();
        SQLIdentifierExpr oldExpr = (SQLIdentifierExpr) tableSource.getExpr();
        String newName = strategy.shardTableName( oldExpr.getSimleName(), params, mapperId );
        tableSource.setExpr( new SQLIdentifierExpr( newName ) );
        return update;
    }

}
